package bookstudytest.lambda;

import bookstudytest.Hello;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

/**
 * @Auther: Lee
 * @Date: 2018/5/31 09:38
 * @Description:
 */
public class LambdaThread {

    public static void main(String[] args) {
//        lambda();
        new LambdaThread().lambdaTest5();

    }

    public static void lambdaTest() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("直接匿名类就完事了！");
            }
        });
        Thread thread1 = new Thread(() -> System.out.println("lambda 开始了？"));
        thread.start();
        thread1.start();
    }

    public void LambdaTest2() {
        JButton show = new JButton("Show");
        show.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Event handling without lambda expression is boring");
            }
        });

        show.addActionListener((ActionEvent e) -> {
            System.out.println("lambda整就完事了");
            System.out.println(e.paramString());
        });
    }

    public void lambdaTest3() {
        List<Hello> hellos = new ArrayList<>();
        Collections.sort(hellos, (h1, h2) -> h1.b.compareTo(h2.b));

    }

    public void lambdaTest4() {
        List features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
//        for (Object a : features){
//            System.out.println(a);
//        }
//        features.forEach(n-> System.out.println(n));
        features.forEach(System.out::println);
    }

    public void lambdaTest5() {
        // 不使用lambda表达式为每个订单加上12%的税
        List costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);
//        for (Object cost : costBeforeTax) {
//            double price =(Integer) cost + .12 *(Integer)cost;
//            System.out.println(price);
//        }
//        // 使用lambda表达式
//        List costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);
        costBeforeTax.stream().map((cost) -> (Integer)cost + .12*(Integer)cost).forEach(System.out::println);
    }

}
